package lib

import (
	"LearnProject/core"
	"LearnProject/models"
	"gorm.io/gorm"
	"time"
)

//根据ID获取帖子的内容 单条
func GetPostItem(id int) *models.PostsModel {
	postItem := models.PostsModel{}
	core.MysqlConnect.Where("id=?", id).First(&postItem)
	return &postItem
}

//查询多条记录
func GetPostList(status int) []*models.PostsModel {
	postList := []*models.PostsModel{}
	core.MysqlConnect.Where("status=?",status).Find(&postList)
	return postList
}

//写入帖子内容
func SavePost(id int, title string) int {

	if id <= 0 {
		postItem := models.PostsModel{
			Title:title,
			CreatedAt:time.Now(),
		}
		core.MysqlConnect.Create(&postItem)
		return postItem.Id

	} else {

		postItem := GetPostItem(id)
		postItem.Title = title
		core.MysqlConnect.Save(&postItem)
		return postItem.Id
	}
}

//删除帖子内容
func PostDelete(id int) *gorm.DB{
	postItem := models.PostsModel{}
	return core.MysqlConnect.Where("id=?", id).Delete(&postItem)
}